﻿using System;
using System.Reflection;
using System.Windows.Forms;
using ProgramMain.Google;
using ProgramMain.Layers;

namespace ProgramMain.OpticNet
{
    public partial class FrmOpticMap : ProgramMain.Framework.frmBase
    {
        public FrmOpticMap()
        {
            InitializeComponent();

            UniqueIdent = GenerateFormIdent();
            mapCtl1.LostFocus += new EventHandler(mapCtl1_LostFocus);
        }

        void mapCtl1_LostFocus(object sender, EventArgs e)
        {
            if(mapCtl1.hotCableTunnels == null)
            {
           //     mapCtl1.Focus();
            }
        }



        private static string GenerateFormIdent()
        {
            return GenerateUniqueIdent(MethodInfo.GetCurrentMethod().DeclaringType.Name);
        }

        public static FrmOpticMap ShowForm()
        {
            string FormIdent = GenerateFormIdent();
            FrmOpticMap frm = (FrmOpticMap) FindForm(FormIdent);
            if (frm != null)
            {
                frm.Activate();
            }
            else
            {
                frm = new FrmOpticMap();
                frm.ShowAsMDI(Program.MainForm);
                frm.RefreshForm();
            }
            return frm;
        }

        private void FrmOpticMap_FormClosing(object sender, FormClosingEventArgs e)
        {
            mapCtl1.ControlClosing();
        }

        public void RefreshForm()
        {
            mapCtl1.RefreshControl();
        }

        public void SetCenterMapObject(decimal longitude, decimal latitude)
        {
            mapCtl1.MoveCenterMapObject(longitude, latitude);
        }

        public void AddFreeLine(NetLayer.FreeLine freeLine)
        {
            mapCtl1.AddFreeLine(freeLine);
        }

        public void AddMapPoint(Coordinate coordinate)
        {
            mapCtl1.AddMapPoint(coordinate);
        }

        public void AddVertesLighted(int vertexID)
        {
            mapCtl1.AddVertesLighted(vertexID);
           
        }

        public void AddCableTunnelLighted(int cableTunnelID)
        {
            mapCtl1.AddCableTunnelLighted(cableTunnelID);
        }

        public void ClearLighted()
        {
            mapCtl1.ClearLighted();
        }

        private void opticNetDesignPanel1_RefreshMapClicked(object sender, EventArgs e)
        {
            RefreshForm();
        }
        
        private void opticNetDesignPanel1_LevelValueChanged(object sender, OpticNet.OpticNetDesignPanel.LevelValueArgs e)
        {
            mapCtl1.Level = e.Level;
        }

        private void opticNetDesignPanel1_CenterMapClicked(object sender, EventArgs e)
        {
            mapCtl1.SetCenterMap();
        }

        private void opticNetDesignPanel1_MouseActionModeChanged(object sender, OpticNetDesignPanel.ActionModeEventArgs e)
        {
            mapCtl1.SetMoveEditMode(e.MouseActionMode, e.MousePopUpMode);
        }

        private void mapCtl1_OnTurnOffEditMode(object sender, EventArgs e)
        {
            opticNetDesignPanel1.PushUpButtons();
        }

        public int Level
        {
            set { opticNetDesignPanel1.level = value; }
        }
        private void mapCtl1_LevelValueChanged(object sender, OpticNetDesignPanel.LevelValueArgs e)
        {
            opticNetDesignPanel1.level = e.Level;
        }

        private void opticNetDesignPanel1_ShowTranzitChange(object sender, EventArgs e)
        {
            if (sender is bool)
            {
                mapCtl1.ShowTranzitVertex((bool)sender);
            }
        }

        private void opticNetDesignPanel1_RulerTypeChanged(object sender, Framework.IntEvent e)
        {
            if (e.ObjectID.HasValue)
            mapCtl1.SetRulerType(e.ObjectID.Value);
        }

        private void opticNetDesignPanel1_ClearMeasureEvent(object sender, EventArgs e)
        {
            mapCtl1.ClearMeasureRule();
        }

        private void mapCtl1_CableTunnelsSizeChanged(object sender, OpticNetCtl.BoolEvent e)
        {
            opticNetDesignPanel1.SetCheckCableTunnels = e.BoolMessage;
        }
    }
}
